<template>
  <view class="container">
    <!-- 标题栏 -->
    <view class="header">
      <view class="back-button" @click="goBack">
        <uni-icons class="back-icon" custom-prefix="iconfont" type="icon-fanhui" size="18"></uni-icons>
      </view>
      <view class="title">{{ title }}</view>
    </view>
    
    <!-- PPT内容区 -->
    <view class="ppt-container">
      <swiper 
        class="ppt-swiper" 
        :current="currentSlide" 
        @change="onSlideChange"
        :indicator-dots="true"
      >
        <swiper-item v-for="(slide, index) in pptSlides" :key="index" class="ppt-slide">
          <image :src="slide.imageUrl" mode="aspectFit" class="slide-image" />
        </swiper-item>
      </swiper>
      
      <!-- 页码指示器 -->
      <view class="pagination">
        {{ currentSlide + 1 }} / {{ pptSlides.length }}
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'

// 获取页面传递的参数
const title = ref('PPT标题')
const contentId = ref('')
const parentId = ref('')

// PPT幻灯片数据
const pptSlides = ref([])
const currentSlide = ref(0)

// 初始化
onMounted(() => {
  // 获取页面参数
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]
  const options = currentPage.$page.options
  
  if (options) {
    contentId.value = options.id || ''
    title.value = decodeURIComponent(options.title || 'PPT详情')
    parentId.value = options.parentId || ''
    
    // 获取PPT数据
    fetchPptData(contentId.value)
  }
})

// 获取PPT数据
const fetchPptData = async (id) => {
  try {
    // 模拟API请求
    // const response = await uni.request({
    //   url: `your-api-endpoint/ppt/${id}`,
    //   method: 'GET'
    // })
    
    // 模拟数据
    pptSlides.value = [
      { imageUrl: '/static/images/ppt-slide1.png' },
      { imageUrl: '/static/images/ppt-slide2.png' },
      { imageUrl: '/static/images/ppt-slide3.png' },
      { imageUrl: '/static/images/ppt-slide4.png' },
      { imageUrl: '/static/images/ppt-slide5.png' }
    ]
    
    console.log('PPT data fetched for id:', id)
  } catch (error) {
    console.error('Failed to fetch PPT data:', error)
    uni.showToast({
      title: '获取PPT内容失败',
      icon: 'none'
    })
  }
}

// 幻灯片变化事件
const onSlideChange = (e) => {
  currentSlide.value = e.detail.current
  
  // 可以在这里记录学习进度
  // updateProgress(contentId.value, currentSlide.value)
}

// 更新学习进度
const updateProgress = async (id, slideIndex) => {
  try {
    // 模拟API请求
    // await uni.request({
    //   url: 'your-api-endpoint/progress',
    //   method: 'POST',
    //   data: {
    //     contentId: id,
    //     slideIndex: slideIndex,
    //     timestamp: new Date().getTime()
    //   }
    // })
    console.log('Progress updated for slide:', slideIndex)
  } catch (error) {
    console.error('Failed to update progress:', error)
  }
}

// 返回上一页
const goBack = () => {
  uni.navigateBack()
}
</script>

<style scoped lang="scss">
.container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-top: var(--status-bar-height);
  background-color: #f5f5f5;
}

.header {
  height: 90rpx;
  display: flex;
  align-items: center;
  padding: 0 30rpx;
  background-color: #ffffff;
  border-bottom: 2rpx solid rgba(229, 229, 229, 1);
}

.back-button {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-icon {
  font-size: 40rpx;
  color: #333;
}

.title {
  font-size: 32rpx;
  font-weight: bold;
  margin-left: 20rpx;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ppt-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  margin: 20rpx;
  background-color: #ffffff;
  border-radius: 12rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.ppt-swiper {
  flex: 1;
  width: 100%;
  height: calc(100vh - 90rpx - var(--status-bar-height) - 40rpx);
}

.ppt-slide {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20rpx;
}

.slide-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.pagination {
  position: absolute;
  bottom: 40rpx;
  right: 40rpx;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  padding: 8rpx 16rpx;
  border-radius: 30rpx;
  font-size: 24rpx;
}
</style> 